import direct.directbase.DirectStart

from pr3da.venv.Environment import World
from pr3da.layout.Layouts import RandomLayout, LineLayout, GridLayout, ForceDirectedLayout
from pr3da.transition.Transitions import FlyTransition, FadeTransition

from pandac.PandaModules import *


# Set up the world
w = World()

from pr3da.control.Management import Pr3daManager
pm = Pr3daManager()

from pr3da.plugins.FileSystemPlugin import FileSystemPlugin
fsp = FileSystemPlugin()
pm.registerPlugin(fsp)

psets = []

purls = pm.search('t_*.jpg')
s = pm.createSet(purls)
l = GridLayout(10,10)
l.setTransition(FlyTransition())
s.setLayout(l)
psets.append(s)

purls = pm.search('m_*.jpg')
s = pm.createSet(purls)
l = LineLayout()
#no transition
s.setLayout(l)
psets.append(s)

purls = pm.search('c_*.jpg')
s = pm.createSet(purls)
l = RandomLayout()
l.setTransition(FlyTransition())
s.setLayout(l)
psets.append(s)

purls = pm.search('c_*i.jpg')
s = pm.createSet(purls)
l = ForceDirectedLayout()
l.setTransition(FlyTransition())
s.setLayout(l)
psets.append(s)

purls = pm.search('bp_*.jpg')
s = pm.createSet(purls)
l = ForceDirectedLayout()
l.setTransition(FlyTransition())
s.setLayout(l)
psets.append(s)

purls = pm.search('*.jpg')
s = pm.createSet(purls)
l = GridLayout(3,3)
l.setTransition(FlyTransition())
s.setLayout(l)
psets.append(s)

n=[0]

def append():
  w.moveCameraToDefaultPos()
  if psets:
    #pm.addSet(psets.pop())
    pm.addSet(psets[n[0]%len(psets)])
    n[0]+=1
  else:
    print("there are no more sets")

def toggleGlow():
  pm.toggleGlow()

def toggleReflect():
  pm.toggleReflect()

def changeLayout(arg):
  aset = pm.getActiveSet()
  
  if arg is 1:
    l = GridLayout(3,3)
    w.moveCamera(Point3(82.5653, -44.2588, 44.4945),
                 VBase3(48.2817, -20.9618, 7.12456))
    l.setTransition(FlyTransition())
  elif arg is 2:
    l = LineLayout((1,0,0))
    w.moveCamera(Point3(-36.3882, -122.903, -4.88624),
                 VBase3(-32.6255, 13.7257, 2.02819))
    l.setTransition(FlyTransition())
  elif arg is 3:
    l = LineLayout((1,1,0.5))
    w.moveCamera(Point3(-36.3882, -122.903, -4.88624),
                 VBase3(-32.6255, 13.7257, 2.02819))
    l.setTransition(FlyTransition())
  elif arg is 4:
    l = LineLayout((0,1,1))
    w.moveCamera(Point3(-36.3882, -122.903, -4.88624),
                 VBase3(-32.6255, 13.7257, 2.02819))
    l.setTransition(FlyTransition())
  elif arg is 5:
    l = RandomLayout()
    w.moveCamera(Point3(59.9807, -46.1487, 16.3604),
                 VBase3(39.8023, -2.84879, 0.500587))
    l.setTransition(FlyTransition())
  elif arg is 6:
    l = GridLayout(10,10)
    w.moveCamera(Point3(-22.1698, -146.338, 16.1798),
                 VBase3(-24.9852, 0.77991, -0.68143))
    l.setTransition(FlyTransition())
  elif arg is 0:
    w.moveCameraToDefaultPos()
    return
  
  aset.setLayout(l)
  pm.reLayout()

def printCameraPos():
  print("Camera Pos: %s" % str(base.camera.getPos()))
  print("Camera Hpr: %s" % str(base.camera.getHpr()))

w.accept('a', append)
w.accept('g', toggleGlow)
w.accept('r', toggleReflect)

w.accept('m', printCameraPos)

w.accept('1', changeLayout, [1])
w.accept('2', changeLayout, [2])
w.accept('3', changeLayout, [3])
w.accept('4', changeLayout, [4])
w.accept('5', changeLayout, [5])
w.accept('6', changeLayout, [6])
w.accept('0', changeLayout, [0])
#w.accept('8', changeLayout, [8])
#w.accept('9', changeLayout, [9])

if __name__ == '__main__':
  from pandac.PandaModules import loadPrcFile
  loadPrcFile("conf/panda3d.conf")
  run()

